﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using TA.Turbo.Controls;

namespace TA.Turbo.Design
{
	/// <summary>
	/// Uses an instance of the specified control ID lister type to retrieve a list of control IDs and returns it as the standard values collection.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public class ControlIdConverter<T> : StringConverter
		where T : BaseControlIdLister, new()
	{
		/// <summary>
		/// Returns a collection of standard values for the data type this type converter is designed for when provided with a format context.
		/// </summary>
		/// <param name="context"></param>
		/// <returns></returns>
		public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
		{
			if (context != null)
			{
				Control ctl = context.Instance as Control;
				if (ctl == null)
				{
					IControlOwned controlOwned = context.Instance as IControlOwned;
					if (controlOwned != null)
					{
						ctl = controlOwned.Owner;
					}
				}

				if (ctl != null)
				{
					string[] controlIDs = new T().GetControlIdList(ctl);

					if (controlIDs.Length > 0)
					{
						return new TypeConverter.StandardValuesCollection(controlIDs);
					}
				}
			}
			return base.GetStandardValues(context);
		}
		/// <summary>
		/// Returns whether the collection of standard values returned from System.ComponentModel.TypeConverter.GetStandardValues() is an exclusive list of possible values, using the specified context.
		/// </summary>
		/// <param name="context"></param>
		/// <returns></returns>
		public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
		{
			return false;
		}
		/// <summary>
		/// Returns whether this object supports a standard set of values that can be picked from a list, using the specified context.
		/// </summary>
		/// <param name="context"></param>
		/// <returns></returns>
		public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
		{
			return true;
		}
	}

}
